/**
 *@description 用户画像》自然人属性》渠道来源
 *@author yhw
 */
import {
  activityQuery,
  sourceQuery
} from '../services/PersonsSource';
import {
  parse
} from 'qs';
import error from '../utils/tips';

export default {

  namespace: 'PersonsSource',

  state: {
    activityData: null,
    activityLoading: false,
    sourceData: null,
    sourceLoading: false,
    occasionData: null,
    occasionLoading: false,
  },

  subscriptions: {},

  effects: { * activityQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'activityLoading'
      });

      const {
        data
      } = yield call(activityQuery, parse(payload));

      if (data) {

        let temp = data.data ? {
            category: [],
            value: []
          } : null,
          colors = ['#FFF1C7', '#B3A9A4', '#DDDAD6', '#F3F2F0', '#FFE5D2'];
        data.data && data.data.map(function(item, index) {
          temp.category.push(item.name);
          temp.value.push({
            name: item.name,
            value: item.value,
            itemStyle: {
              normal: {
                color: colors[index]
              }
            }
          });
        });

        yield put({
          type: 'activityQuerySuccess',
          payload: {
            activityData: temp,
          },
        });
      } else {
        error("获取按活动失败");
      }
    },
    //===================================
    * sourceQuery({
      payload
    }, {
      call,
      put
    }) {

      yield put({
        type: 'sourceLoading'
      });

      const {
        data
      } = yield call(sourceQuery, parse(payload));

      if (data) {

        let temp = {
            category: [],
            value: []
          },
          colors = ['#ceb180', '#fff2cc', '#fbe5d6', '#f8cbad', '#ededed', '#deebf7', '#bdd7ee', '#adb9ca', '#f2f2f2', '#d9d9d9', '#ffc86b', '#ffb22f'];
        data.data && data.data.map(function(item, index) {
          temp.category.push(item.name);
          temp.value.push({
            name: item.name,
            value: item.value,
            itemStyle: {
              normal: {
                color: colors[index]
              }
            }
          });
        });

        yield put({
          type: 'sourceQuerySuccess',
          payload: {
            sourceData: temp,
          },
        });
      } else {
        error("获取按渠道失败");
      }
    },
  },

  reducers: {
    activityLoading(state) {
      return {...state,
        activityLoading: true
      };
    },
    activityQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        activityLoading: false
      };
    },
    //===================================
    sourceLoading(state) {
      return {...state,
        sourceLoading: true
      };
    },
    sourceQuerySuccess(state, action) {
      return {...state,
        ...action.payload,
        sourceLoading: false
      };
    },
  },

};